﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Xml.Serialization;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using PhoenixCrabV6.Engine.Images;

namespace PhoenixCrabV6.Engine.IO.Interface
{
    [DataContract]
    [KnownType(typeof(MultiImage))]
    public class InterfaceElement : IInterfaceElement
    {
        public event EventHandler OnLeftClicked;
        public event EventHandler OnRightClicked;
        public event EventHandler OnMiddleClicked;
        
        /// <summary>
        /// Images used in this InterfaceElement
        /// </summary>
        [XmlElement ("Images", typeof(MultiImage))]
        [DataMember(Name = "Images")]
        public IMultiImage Images { get; set; }
        
        /// <summary>
        /// The area on the screen where the mouse can interact with this InterfaceElement
        /// </summary>
        public Rectangle ClickableArea {
            get
            {
                if (clickableArea == Rectangle.Empty)
                {
                    clickableArea =
                            new Rectangle((int)Images.Position.X, (int)Images.Position.Y, Images[0].Width, Images[0].Height);
                }
                return clickableArea;
            }
            set
            {
                clickableArea = value;
            } 
        }

        private Rectangle clickableArea;

        [XmlIgnore]
        public Texture2D BaseImage
        {
            get { return Images[0]; }
        }

        [XmlIgnore] public Texture2D MouseOverImage
        {
            get { return Images[1]; }
        }

        [XmlIgnore] public Texture2D MouseClickImage
        {
            get { return Images[2]; }
        }
       
        /// <summary>
        /// The image that currently gets displayed
        /// </summary>
        [XmlIgnore] public Texture2D DisplayImage
        {
            get
            {
                return Images.DisplayImage;

                /*if (MouseInClickableArea())
                {
                    if (InputManager.Instance.LeftClick())
                        return MouseClickImage;
                    else
                        return MouseOverImage;
                }
                else
                    return BaseImage;*/
            }
            set { }
        }

        [DataMember]
        public Vector2 Position { get; set; }

        [DataMember]
        public float Alpha { get; set; }

        [DataMember]
        public Vector2 Scale { get; set; }



        /// <summary>
        /// Checks if mouse cursor currently in clickable area
        /// </summary>
        /// <returns>If mouse currently in clickable area</returns>
        public bool MouseInClickableArea()
        {

            //todo check to see also that the moused over area/point is not alpha mask 0 (transparent)
            //make sure not clicking on blank area of image
            //use this to have by-pixel selection

            return ClickableArea.Contains(new Point(
                (int)InputManager.Instance.CurrentMousePosition().X, 
                (int)InputManager.Instance.CurrentMousePosition().Y));
        }

        public void LeftClick()
        {
            
                OnLeftClicked(this, null);
        }

        public void RightClick()
        {
                OnRightClicked(this, null);
        }

        public void MiddleClick()
        {
                OnMiddleClicked(this, null);
        }

        public void LoadContent()
        {
            Images.LoadContent();
            
        }

        public void UnloadContent()
        {
            Images.UnloadContent();
        }

        public void Update(GameTime gameTime)
        {
            if (MouseInClickableArea())
            {
                if (InputManager.Instance.LeftClick())
                    Images.DrawAtIndex = 2;
                else
                    Images.DrawAtIndex = 1;
            }
            else
                Images.DrawAtIndex = 0;
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            Images.Draw(spriteBatch);
        }
    }
}
